<template>
  <uni-nav-bar title="仓库列表" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
  <!-- 切换 -->
  <view class="package" style="padding:0px;">
    <z-tabs ref="tabs" :list="list" :current="current" @change="tabsChange" inactive-color="#333" />
  </view>
  <scroll-view scroll-y="true" enable-back-to-top @scrolltolower="loadMore" class="scroll-box"
    :style="{ height: pageHeight + 'px' }">
    <view class="package space-between" @click="navTo('administrator/storehouse/storeDeatil?id='+item.id)"
      v-for="(item,index) in frinedList" :key="item.id" :style="{marginTop:index==0?'0px':'15rpx',minHeight:'100rpx'}">
      <view>{{item.name}}</view>
      <uni-icons type="forward" size="20"></uni-icons>
    </view>
    <Popu v-if="frinedList.length<1" :pageHeight="pageHeight" text="暂无仓库列表"></Popu>
  </scroll-view>
</template>

<script setup lang="ts">
  import { ref } from 'vue';
  import { navBack, navTo } from '@/utils/navigator';
  import { showLoading } from '@/utils/prompt';
  import Popu from '@c/earthPushing/common/popu.vue';
  import { getKuCate, getKuList } from '@/gql/warehouse';
  import { toPublish } from '@mqtt';
  /**
   * 仓库列表
   * @param list 仓库类型信息
   * @param current 选项索引-
   * @param listID 仓库类型ID
   * @param pageHeight 页面高度
   * @param frinedList 仓库列表
   * @param lastPage 总页数
   * @param currentPage 当前页数
   */
  const list = ref([])
  const current = ref(0)
  const listID = ref()

  const pageHeight = ref();
  const frinedList = ref([])
  const lastPage = ref(1);
  const currentPage = ref(1);
  uni.getSystemInfo({
    success: function (res) {
      pageHeight.value = res.windowHeight - 120;
    },
  });
  init()
  /**
   * 获取仓库类型列表
   */
  function init() {
    showLoading('正在加载');
    const payload = {
      query: getKuCate
    };
    toPublish('ql/control/getKuCate', payload, (obj : any) => {
      uni.hideLoading();
      const { data } = obj;
      list.value = data.getKuCate;
      listID.value = data.getKuCate && data.getKuCate[0].id
      wareList()
    });
  }
  /**
   * 获取仓库信息
   * @param {Number} page  获取仓库列表数据
   * @param {Number} cid  仓库类型ID
   */
  function wareList() {
    showLoading()
    const payload = {
      query: getKuList,
      variables: {
        page: currentPage.value,
        cid: Number(listID.value)
      },
    };
    toPublish(
      'ql/control/getKuList',
      payload,
      (obj : any) => {
        const { getKuList } = obj.data;
        frinedList.value = [...frinedList.value, ...getKuList.list];
        lastPage.value = getKuList.lastpage;
      }
    );
  }
  /**
   * 加载更多
   */
  function loadMore() {
    if (currentPage.value < lastPage.value) {
      currentPage.value++;
      wareList();
    }
  }
  //tabs通知swiper切换
  function tabsChange(index : number) {
    current.value = index;
    listID.value = list.value[index].id
    currentPage.value = 1
    frinedList.value = []
    wareList();
  }
</script>

<style scoped lang="less">
</style>
<style>
	.package>>>.z-tabs-item {
		flex: none !important;
	}
</style>